package com.five.monkey.feign.es.convert;

import com.five.monkey.vo.PageVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
//import org.springframework.data.elasticsearch.core.SearchHit;
//import org.springframework.data.elasticsearch.core.SearchPage;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 狻猊
 * @date 2021-11-23 20:53:49
 */
public class PageConvert<T, Tvo> {

    /**
     * 结果转换。例如：Page<User> --> PageVo<UserVo>
     * @author 狻猊
     * @param page 分页数据
     * @param operateT T类型 --> Tvo类型的转换函数
     * @return com.five.monkey.vo.PageVo<Tvo>
     * @date 2021/11/26 11:03 上午
     */
    public PageVo<Tvo> page2vo(Page<T> page, Function<T, ? extends Tvo> operateT) {
        PageVo<Tvo> vo = new PageVo<>();
        Pageable pageable = page.getPageable();
        List<Tvo> content = page.getContent().stream().map(operateT).collect(Collectors.toList());
        vo.setContent(content).setPageNumber(pageable.getPageNumber() + 1)
                .setPageSize(pageable.getPageSize()).setOffset(pageable.getOffset())
                .setFirst(page.isFirst()).setLast(page.isLast()).setTotalPages(page.getTotalPages())
                .setTotalElements(page.getTotalElements()).setSize(content.size());
        return vo;
    }

    /**
     * 结果转换。例如：SearchPage<User> --> PageVo<UserVo>
     * @author 狻猊
     * @param searchPage 分页数据
     * @param operateT T类型 --> Tvo类型的转换函数
     * @return com.five.monkey.vo.PageVo<Tvo>
     * @date 2021/11/26 11:07 上午
     */
    //public PageVo<Tvo> searchPage2vo(SearchPage<T> searchPage, Function<T, ? extends Tvo> operateT) {
    //    PageVo<Tvo> vo = new PageVo<>();
    //    Pageable pageable = searchPage.getPageable();
    //    List<Tvo> content = searchPage.getContent().stream().map(SearchHit::getContent).map(operateT).collect(Collectors.toList());
    //    vo.setPageNumber(pageable.getPageNumber()).setPageSize(pageable.getPageSize())
    //            .setOffset(pageable.getOffset()).setFirst(searchPage.isFirst())
    //            .setLast(searchPage.isLast()).setTotalPages(searchPage.getTotalPages())
    //            .setTotalElements(searchPage.getTotalElements()).setSize(content.size())
    //            .setContent(content);
    //    return vo;
    //}
}
